<html>
<head>
<title>Appointment Scheduling System</title>

<link href="./style.css" rel="stylesheet" type="text/css" media="screen" />
<style>
</style>
<script>
function validateForm()
{
var x=document.forms["myForm"]["word"].value;
if (x==null || x=="")
  {
  alert("Please Enter a Word to Search!");
  return false;
  }
}
</script>

</head>
<body>
<?php
//	include "dbconnect.php"
	date_default_timezone_set('GMT');
	$dateTimeObj = new DateTime();
	$curTime = new DateTime();
	$curTimestamp = intval($curTime->getTimestamp());
//	$curTime = DateTime::createFromFormat("d/m/Y H:i", time());
//	$curTimestamp = $curTime->getTimestamp();
	error_reporting(0);
	session_start();
	if(isset($_SESSION['email']))
	{	

?>
<div id='body_container'>
	<div id="header"><div id='banner'>NITC Appoinmtent Scheduling System</div></div>
	<div id='menu'>
	<div id="tabs"><a href="home.php">Home</a><a href="online.php">Available Faculties</a><a href="message.php">Request Appointment</a><a href="logout.php">Logout</a></div>
	</div>
	<div id='page_container'>
	<div id="page">
	<div id='notifications' class='page_contents'>
	<?php


	$con=mysql_connect('localhost','root','root') or die('server not found');
	$db = mysql_select_db("webphp",$con) or die('db not found');
	$curDate = date('d/m/Y', time());
	$curTime = date('H:i', time());
	$uid1 = $_SESSION['email'];
	$qry = "select * from msglog where receiver='$uid1' and status = 0";
	$exeqry = mysql_query($qry);
	echo '<div id="requests" class="notification_kind"><fieldset name="requests"><legend>Requests</legend>';
	$counter = 0;
	while($getqry = mysql_fetch_array($exeqry))
		{
			$counter++;
			echo '<div class="request">';
			$appid = $getqry['reqId'];
			echo '<fieldset name="notifications">';
			echo"<legend> Request No.: " . $counter .  "</legend>";
			$u1 = $getqry['sender'];
			$msg = $getqry['message'];
			$timestamp = $getqry['timestamp'];
			$dateTimeObj->setTimestamp($timestamp);
			$expire=time()+800;
			setcookie("user",$appid,$expire);
		   
			echo "<b>Sender</b>: ".$u1."<br /><b>Message</b>: ".$msg. "<br /><b>Date</b>: " . $dateTimeObj->format("d/m/Y") . "  <b>Time</b>: " . $dateTimeObj->format("H:i");

		    


		echo'	
				<form name="myForm" action="home.php" onSubmit="return validateForm()" method="post">
					<p>
					<input type="submit" name="accept" value="Accept">
					<input type="submit" name="reject" value="Reject">
					
					</p>
				</form>
			</fieldset>
			</div>';
		}
	 	
	echo "</fieldset></div>";

	echo "<div id='appointments' class='notification_kind'>";
	echo '<fieldset name="Appointments">';
	echo "<legend>Current Appointments</legend>";
	$qrappl = "select * from msglog where receiver='$uid1' or sender='$uid1' and status=1";
	$applist = mysql_query($qrappl) or die("Error in fetching Appointment List" . mysql_error());
    echo "<ul>";
	while($getapplist = mysql_fetch_array($applist))
		{
			$senrec = $getapplist['sender'];
			$recsen = $getapplist['receiver'];
			$timestamp = intval($getapplist['timestamp']);
			$dateTimeObj->setTimestamp($timestamp);
			$reqstid = $getapplist['reqId'];
			$stat = $getapplist['status'];
			if($curTimestamp > $timestamp){continue;}
			if($senrec == $uid1 and $stat==1)	
				 {  //echo "".$reqstid;
				 echo "<li>". " Appointment with: <b>" . $recsen . "</b> on  <b>" . $dateTimeObj->format('d/m/Y') . "</b> at <b>" . $dateTimeObj->format('H:i')  . "</b></li >";
				 
				 }
			elseif($recsen == $uid1 and $stat==1)
				 {   //echo "".$reqstid;
			     echo "<li>". " Appointment with: <b>" . $senrec . "</b> on  <b>" . $dateTimeObj->format('d/m/Y') . "</b> at <b>" . $dateTimeObj->format('H:i')  . "</b></li >";

				 }
			 	}
     echo "</ul>" ;

	echo "</fieldset>";
	echo "</div>";

	echo "<div id='rejected' class='notification_kind'>";
	echo '<fieldset name="notifications">';
	echo "<legend>Rejected Appointments</legend>";
	$rejqr = "select * from msglog where sender='$uid1' and status=2 ";
	$getrej = mysql_query($rejqr) or die("Error in getting reject list.." . mysql_error());
	
    $var=10;
    echo "<ul>";
	while(($getrejlist = mysql_fetch_array($getrej)) && ($var>0))
		{
			$timestamp = $getrejlist['timestamp'];
			$dateTimeObj->setTimestamp($timestamp);
			$rejname = $getrejlist['receiver'];
			$rejdate = $getrejlist['dateTime'];
			$appID = $getrejlist['reqId'];

			echo  "<li><b>".$rejname ."</b> rejected appointment for the date: <b>" . $dateTimeObj->format('d/m/Y') .  "</b></li>";
			$var--;

		}
	echo "</ul>";
	echo "</fieldset>";
	echo "</div>";
	echo "</div>";


	if(isset($_POST['accept']))
		{    $v=$_COOKIE["user"];
			$accpq = "UPDATE msglog SET status=1 WHERE  reqId=".$v;	
			echo "Welcome " . $v . "!<br>";
			mysql_query($accpq) or die("Error in Accepting..".mysql_error());	

			echo "<script>alert('Accepted');</script>";
			header("Location:home.php");
			
		}
	if(isset($_POST['reject']))
		{  $v=$_COOKIE["user"];
			$rejq = "UPDATE msglog SET status=2 WHERE reqId=".$v;	
			
			mysql_query($rejq) or die("Error in Accepting..".mysql_error());	

			echo "<script>alert('Rejected');</script>";
			header("Location:home.php");

		}

		
	?>

		
	</div><div class='clear'></div></div>
	<?php
	}
	else
		{
			header("location:index.php");
		}
	?>
	<?php include 'footer.php'; ?>
	</div>
	</div>
</body>
</html>
